//
//  YFShopAdView.m
//  QuanJieShopProject
//
//  Created by apple on 2018/4/14.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFShopAdViewCell.h"

#import "YFShopAdView.h"
#import "YFShopAdViewCell.h"

@interface YFShopAdView()
{
    CGFloat _minimumInteritemSpacing; // item 水平间距
    CGFloat _minimumLineSpacing;      // item 垂直间距
    CGSize _size;       // item的size
    UIEdgeInsets _insets;  // UIEdgeInsets
}
@property (nonatomic, strong) UICollectionView *collectionView;
@end


@implementation YFShopAdView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        // 重新计算高度
        CGFloat cellW = (UISCREEN_WIDTH - 10*4)/3.5;
//        CGFloat cellH = 257.f/1334.f * UISCREEN_HEIGHT;
        _minimumInteritemSpacing = 15.f/750.f *UISCREEN_WIDTH;
        _minimumLineSpacing = _minimumInteritemSpacing;
        CGFloat left_right_padding = (UISCREEN_WIDTH - cellW*2 - _minimumInteritemSpacing)/2;
        CGFloat top_bottom_padding = 30.f/1334.f * UISCREEN_HEIGHT;
        
        _size = CGSizeMake(cellW, frame.size.height);
        _insets = UIEdgeInsetsMake(top_bottom_padding, left_right_padding, top_bottom_padding, left_right_padding);
        
        CGFloat H = top_bottom_padding*2 + frame.size.height*2 + _minimumLineSpacing;
//        self.mj_h = H;
        
        [self makeView];
    }
    return self;
}

- (void)makeView {
    
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    
    //2.初始化collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor clearColor];
    
    //3.注册collectionViewCell
    [self.collectionView registerClass:[YFShopAdViewCell class] forCellWithReuseIdentifier:@"cellId"];
    
    //4.设置代理
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

}

- (void)setAdArray:(NSArray *)adArray {
    
//    // 测试
//    NSMutableArray *tempArr = [NSMutableArray arrayWithArray:adArray];
//    for (id obj in adArray) {
//        [tempArr addObject:obj];
//    }
//    _adArray = tempArr;
//    // 测试结束
    
    _adArray = adArray;
    
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.adArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    YFShopAdViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor yellowColor]
    if (indexPath.row < self.adArray.count) { // 数组count可能没有4个
        cell.model = self.adArray[indexPath.row];
    }
//    cell.backgroundColor = [UIColor redColor];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
     NearShopModel *model = self.adArray[indexPath.row];
    if (self.adImgViewClickBlock) {
        self.adImgViewClickBlock(model);
    }
}

//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return _size;
}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
//    return _insets;
    return UIEdgeInsetsMake(0, 10, 0, 0);
}
//
// 设置每个item水平间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 50;
////    return _minimumInteritemSpacing;
//}
//
////设置每个item垂直间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
//{
//    return  _minimumLineSpacing;
//}




@end
